package com.sap.tpcd.parserengine.wsdlfile;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.net.URI;

import javax.xml.namespace.QName;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.apache.neethi.PolicyComponent;
import org.apache.neethi.PolicyEngine;
import org.apache.woden.ErrorReporter;
import org.apache.woden.WSDLException;
import org.apache.woden.XMLElement;
import org.apache.woden.internal.ErrorLocatorImpl;
import org.apache.woden.internal.util.dom.DOM2Writer;
import org.apache.neethi.Constants;
import org.apache.woden.wsdl20.WSDLComponent;
import org.apache.woden.wsdl20.extensions.BaseComponentExtensionContext;
import org.apache.woden.wsdl20.extensions.ComponentExtensionContext;
import org.apache.woden.wsdl20.extensions.ExtensionElement;
import org.apache.woden.wsdl20.extensions.ExtensionProperty;
import org.apache.woden.wsdl20.extensions.soap.SOAPConstants;
import org.apache.woden.wsdl20.xml.WSDLElement;
import org.w3c.dom.Element;


/**
 * The Class PolicyExtensionsImpl.
 * 
 * @author sagara Gunathunga (sagaragu@gmail.com)
 */
public class PolicyExtensionsImpl implements PolicyExtension {

	/** The source. */
	XMLElement source;
	
	/** The extension type. */
	QName extensionType;
	
	/** The required. */
	boolean required;

	/* (non-Javadoc)
	 * @see org.apache.woden.wsdl20.extensions.ExtensionElement#getExtensionType()
	 */
	public QName getExtensionType() {

		return extensionType;
	}

	/* (non-Javadoc)
	 * @see org.apache.woden.wsdl20.extensions.ExtensionElement#isRequired()
	 */
	public Boolean isRequired() {
		return required;
	}

	/* (non-Javadoc)
	 * @see org.apache.woden.wsdl20.extensions.ExtensionElement#setExtensionType(javax.xml.namespace.QName)
	 */
	public void setExtensionType(QName qname) {
		extensionType = qname;

	}

	/* (non-Javadoc)
	 * @see org.apache.woden.wsdl20.extensions.ExtensionElement#setRequired(java.lang.Boolean)
	 */
	public void setRequired(Boolean required) {
		this.required = required;

	}

	/**
	 * Gets the property.
	 * 
	 * @param propertyName the property name
	 * 
	 * @return the property
	 */
	public ExtensionProperty getProperty(String propertyName) {

		return null;

	}

	/**
	 * Gets the properties.
	 * 
	 * @return the properties
	 */
	public ExtensionProperty[] getProperties() {

		return null;
	}

	/* (non-Javadoc)
	 * @see org.apache.woden.wsdl20.extensions.samples.policy.PolicyExtension#getXMLElement()
	 */
	public XMLElement getXMLElement() {

		return source;

	}

	/* (non-Javadoc)
	 * @see org.apache.woden.wsdl20.extensions.samples.policy.PolicyExtension#setXMLElement(org.apache.woden.XMLElement)
	 */
	public void setXMLElement(XMLElement source) {
		this.source = source;

	}

	/* (non-Javadoc)
	 * @see org.apache.woden.wsdl20.extensions.samples.policy.PolicyExtension#getPolicy()
	 */
	public PolicyComponent getPolicy() {
		Element policyEle = (Element) getXMLElement().getSource();
		if (Constants.URI_POLICY_NS.equals(policyEle.getNamespaceURI())) {
			ByteArrayInputStream inSt = null;
			String nodeString = DOM2Writer.nodeToString(policyEle);
			inSt = new ByteArrayInputStream(nodeString.getBytes());
			if (Constants.ELEM_POLICY.equals(policyEle.getLocalName())) {
				return PolicyEngine.getPolicy(inSt);

			} else if (Constants.ELEM_POLICY_REF.equals(policyEle
					.getLocalName())) {
				return PolicyEngine.getPolicyReferene(inSt);
			}
		}
		throw new IllegalArgumentException(
				"Agrument is neither a <wsp:Policy> nor a <wsp:PolicyReference> element");

	}

}
